import 'package:flutter/material.dart';
import 'package:flutter_ume/common/component_index.dart';
import 'package:flutter_ume/data/model/login_entity.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPage createState() => _LoginPage();
}

class _LoginPage extends State<LoginPage> {
  //手机号的控制器
  TextEditingController phoneEditingController;

  //密码控制器
  TextEditingController passwordEditingController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    phoneEditingController = TextEditingController();
    passwordEditingController = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: _titleWidget(),
      body: _bodyWidget(),
    );
  }

  Widget _titleWidget() {
    return PreferredSize(
        child: AppBar(
            title: Text(
              '登录',
              style: TextStyles.titleTextStyle,
            ),
            backgroundColor: Colours.white,
            centerTitle: true,
            leading: GestureDetector(
              onTap: () => Navigator.pop(context),
              child: Container(
                child: Image.asset(
                  Utils.getImgPath('icon_left'),
                ),
                padding: EdgeInsets.fromLTRB(0.0, Dimens.appBarLeftPadding, 0.0,
                    Dimens.appBarLeftPadding),
              ),
            )),
        preferredSize: Size.fromHeight(Dimens.appBarHeight));
  }

  Widget _bodyWidget() {
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.all(5.0),
      child: Column(
        children: <Widget>[
          TextField(
            maxLength: 11,
            controller: phoneEditingController,
            keyboardType: TextInputType.number,
            decoration: InputDecoration(
                counterText: '',
                contentPadding: EdgeInsets.all(10.0),
                border: OutlineInputBorder(),
                prefixIcon: Icon(Icons.phone),
                labelText: '请输入您的用户名'),
            autofocus: false,
          ),
          Container(
            margin: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 0.0),
            child: TextField(
                controller: passwordEditingController,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  contentPadding: EdgeInsets.all(10.0),
                  prefixIcon: Icon(Icons.lock),
                  labelText: '请输入密码',
                ),
                obscureText: true),
          ),
          RaisedButton(
            onPressed: _login,
            padding: EdgeInsets.all(0.0),
            child: Text(
              '登录',
              style: TextStyle(),
            ),
          )
        ],
      ),
    );
  }

  Future _login() async {
    if (phoneEditingController.text.length != 11) {
      ToastUtil.showShortToast("手机号码格式不对");
    } else if (passwordEditingController.text.length == 0) {
      ToastUtil.showShortToast("请填写密码");
    } else {
      final _params = {
        'mobile': phoneEditingController.text,
        'password': passwordEditingController.text
      };
      //开始登录
      var response = await NetUtils.postNoHead(UmeApi.LOGIN, params: _params);

      var data = json.decode(response.toString());
      LoginEntity loginEntity = LoginEntity.fromJson(data);
      print('retObject.retObject.token:${loginEntity.retObject.token}');
      SpUtil.putString(SpKeys.userToken, loginEntity.retObject.token);
      ToastUtil.showShortToast(Ids.loginSucess);
      Navigator.pop(context, StateCode.code00);

      //   phoneEditingController.clear();
    }
  }
}
